<?php
/*
 * MiniMe - a minimalistic OStatus complient microblogging tool
 * Copyright (C) 2010, Tuomas Koski - koski.tuomas@gmail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @author     Tuomas Koski <koski.tuomas@gmail.com>
 * @copyright  Tuomas Koski
 * @license    http://www.gnu.org/licenses/lgpl.txt
 */
class Mm_Atom_Element_Feed_Builder extends Mm_Xml_Element_Builder_Abstract {
    
    protected $mandatoryChildElements = array(
        array('key' => Mm_Atom_Element_Feed::ELEMENT_ID, 'type' => 1, 'builder' => 'Mm_Atom_Element_Id', 'xpath_query' => '/atom:feed/atom:id[1]'),
        array('key' => Mm_Atom_Element_Feed::ELEMENT_TITLE, 'type' => 1, 'builder' => 'Mm_Atom_Element_Title', 'xpath_query' => '/atom:feed/atom:title[1]'),
    );
    
    protected $optionalChildElements = array(
        array('key' => Mm_Atom_Element_Feed::ELEMENT_UPDATED, 'type' => 1, 'builder' => 'Mm_Atom_Element_Updated', 'xpath_query' => '/atom:feed/atom:updated[1]'),
        array('key' => Mm_Atom_Element_Feed::ELEMENT_GENERATOR, 'type' => 1, 'builder' => 'Mm_Atom_Element_Generator', 'xpath_query' => '/atom:feed/atom:generator[1]'),
        array('key' => Mm_Atom_Element_Feed::ELEMENT_SUBTITLE, 'type' => 1, 'builder' => 'Mm_Atom_Element_Subtitle', 'xpath_query' => '/atom:feed/atom:subtitle[1]'),
        array('key' => Mm_Atom_Element_Feed::ELEMENT_LOGO, 'type' => 1, 'builder' => 'Mm_Atom_Element_Logo', 'xpath_query' => '/atom:feed/atom:logo[1]'),
        array('key' => Mm_Atom_Element_Feed::ELEMENT_ENTRIES, 'type' => 2, 'builder' => 'Mm_Atom_Element_Entry_Builder', 'xpath_query' => '/atom:feed/atom:entry', 'can_have_multiple_items' => true),
        array('key' => Mm_Atom_Element_Feed::ELEMENT_AUTHOR, 'type' => 2, 'builder' => 'Mm_Atom_Element_Author_Builder', 'xpath_query' => '/atom:feed/atom:author[1]'),
        array('key' => Mm_Atom_Element_Feed::ELEMENT_ACTIVITY_SUBJECT, 'type' => 2, 'builder' => 'Mm_Atom_Element_Activitystream_Subject_Builder', 'xpath_query' => '/atom:feed/activity:subject[1]'),
        array('key' => Mm_Atom_Element_Feed::ELEMENT_LINK_HUB, 'type' => 1, 'builder' => 'Mm_Atom_Element_Link', 'xpath_query' => '/atom:feed/atom:link[@rel="hub"][1]'),
        array('key' => Mm_Atom_Element_Feed::ELEMENT_LINK_SALMON, 'type' => 1, 'builder' => 'Mm_Atom_Element_Link', 'xpath_query' => '/atom:feed/atom:link[@rel="salmon"][1]'),
        array('key' => Mm_Atom_Element_Feed::ELEMENT_LINK_SALMON_REPLIES, 'type' => 1, 'builder' => 'Mm_Atom_Element_Link', 'xpath_query' => '/atom:feed/atom:link[@rel="http://salmon-protocol.org/ns/salmon-replies"][1]'),
        array('key' => Mm_Atom_Element_Feed::ELEMENT_LINK_SALMON_MENTION, 'type' => 1, 'builder' => 'Mm_Atom_Element_Link', 'xpath_query' => '/atom:feed/atom:link[@rel="http://salmon-protocol.org/ns/salmon-mention"][1]'),
        array('key' => Mm_Atom_Element_Feed::ELEMENT_LINK_ALTERNATE, 'type' => 1, 'builder' => 'Mm_Atom_Element_Link', 'xpath_query' => '/atom:feed/atom:link[@rel="alternate" and @type="text/html"][1]'),
    );
    
    public function __construct($rawXML) {
        $this->rawXML = $rawXML;
    }
    
    /**
     * @return Mm_Atom_Element_Feed
     */
    public function build() {
        $this->parseChildElementsFromRawXML();
        return new Mm_Atom_Element_Feed($this->childElements, $this->namespaces);
    }
    
}